require File.dirname(__FILE__) +  '/regex_builder'

class RegexUtil

    class IP
        include RegexpBuilder

        def less_than_255
            _0_to_100 = repeat(digit(), 1, 2)
            _100_to_200 = "1" + repeat(digit(), 2)
            _200_to_250 = "2" + either("0-4") + either("0-9")
            _250_to_255 = "25" + either("0-4")
            one_of(_0_to_100, _100_to_200, _200_to_250, _250_to_255)
        end

        def initialize
            @ip_pattern = repeat(group(group(less_than_255()) + literal(".")), 3) + group(less_than_255())
        end

        attr_reader :ip_pattern

    end


    class Email
        include RegexpBuilder

        def before_at
            at_least_once(word_boundary(either("a-z0-9.%+-")))
        end

        def after_at
            at_least_once(either("a-z0-9.-")) + literal(".") + repeat(either("a-z"), 2, 4) + word_boundary("")
        end

        def initialize
            @email_pattern = before_at() + "@" + after_at()
        end

        attr_reader :email_pattern

    end

    Email_Pattern = Regexp.new(Email.new.email_pattern, "i")
    IP_Pattern = Regexp.new(IP.new.ip_pattern)

end
